package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "物资类别")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Resource
    private ProductCategoryService productCategoryService;

    //查询所有
    @GetMapping("/categoryTree")
    public ResponseBean categoryTree(Integer pageNum, Integer pageSize) {
        List<ProductCategoryTreeNodeVO> list = productCategoryService.getCategoryTree();
        List<ProductCategoryTreeNodeVO> page;
        Map map = new HashMap();
        if (pageNum != null && pageSize != null) {
            page = ListPageUtils.page(list, pageSize, pageNum);
            map.put("rows", page);
        } else {
            map.put("rows", list);
        }

        map.put("total", list.size());
        return ResponseBean.success(map);
    }

    //添加时查询父类
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> parentCategoryTree = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(parentCategoryTree);
    }

    //添加
    @PostMapping("/add")
    public ResponseBean addProductCategory(@RequestBody ProductCategory productCategory) {
        Integer integer = productCategoryService.addProductCategory(productCategory);
        if (integer != 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("添加失败");
        }
    }

    //回显
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        ProductCategory productCategory = productCategoryService.edit(id);
        return ResponseBean.success(productCategory);
    }

    //修改
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody ProductCategory productCategory) {
        Integer update = productCategoryService.update(id, productCategory);
        return ResponseBean.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        Integer result = productCategoryService.delete(id);
        if (result == 0) {
            return ResponseBean.success();
        }
        Map map = new HashMap();
        map.put("errorMsg", "不能删除");
        return ResponseBean.error(map);
    }

}


